package com.mjh.zhujianju.service.supervise.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.github.pagehelper.PageHelper;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.model.SysUser;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.SysUserService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.enums.EStatus;
import com.livew.framework.exception.XTBusinessException;
import com.livew.framework.page.PagedBeanUtil;
import com.livew.framework.page.PagedResult;
import com.livew.framework.sql.XTSqlUtils;
import com.livew.framework.utils.XTCurrentUser;
import com.livew.framework.utils.XTEntityUtils;
import com.livew.framework.utils.XTListUtils;
import com.livew.framework.utils.XTStringUtils;
import com.mjh.zhujianju.dao.supervise.SupervisionMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.enums.SexEnum;
import com.mjh.zhujianju.enums.SupervisionEstimateEnum;
import com.mjh.zhujianju.enums.SupervisionLevelEnum;
import com.mjh.zhujianju.enums.SupervisionScheduleEnum;
import com.mjh.zhujianju.enums.SupervisionTypeEnum;
import com.mjh.zhujianju.model.agency.DepartmentEO;
import com.mjh.zhujianju.model.base.CommentEO;
import com.mjh.zhujianju.model.base.ContentEO;
import com.mjh.zhujianju.model.supervise.SupervisionEO;
import com.mjh.zhujianju.model.supervise.SupervisionEstimateEO;
import com.mjh.zhujianju.model.supervise.SupervisionScheduleEO;
import com.mjh.zhujianju.model.supervise.SupervisionUsersEO;
import com.mjh.zhujianju.model.visit.VisitUserEO;
import com.mjh.zhujianju.service.agency.DepartmentService;
import com.mjh.zhujianju.service.agency.DeptUserService;
import com.mjh.zhujianju.service.base.CommentService;
import com.mjh.zhujianju.service.base.ContentService;
import com.mjh.zhujianju.service.supervise.SupervisionEstimateService;
import com.mjh.zhujianju.service.supervise.SupervisionScheduleService;
import com.mjh.zhujianju.service.supervise.SupervisionService;
import com.mjh.zhujianju.service.supervise.SupervisionUsersService;
import com.mjh.zhujianju.service.visit.VisitUserService;
import com.mjh.zhujianju.util.FileUrlAdapter;
import com.mjh.zhujianju.vo.agency.DeptUserVO;
import com.mjh.zhujianju.vo.supervision.StatisticsVO;
import com.mjh.zhujianju.vo.supervision.SupervisionEstimateVO;
import com.mjh.zhujianju.vo.supervision.SupervisionScheduleVO;
import com.mjh.zhujianju.vo.supervision.SupervisionVO;
import com.mjh.zhujianju.vo.visit.VisitUserVO;

@Service
public class SupervisionServiceImpl extends BaseServiceImpl<SupervisionEO> implements SupervisionService {

	@Resource
	private SupervisionMapper supervisionMapper;

	@Resource
	private SupervisionUsersService supervisionUsersService;

	@Resource
	private FileService fileService;

	@Autowired
	private ContentService contentService;

	@Autowired
	private VisitUserService visitUserService;

	@Autowired
	private DepartmentService departmentService;

	@Autowired
	private SupervisionEstimateService supervisionEstimateService;

	@Autowired
	private SupervisionScheduleService supervisionScheduleService;

	@Autowired
	private SysUserService sysUserService;

	@Autowired
	private CommentService commentService;
	
	@Autowired
	private DeptUserService deptUserService;

	public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

	public SupervisionServiceImpl(SupervisionMapper supervisionMapper) {
		super(supervisionMapper);
	}


	@Override
	@Transactional
	public void saveInfo(SupervisionVO vo) throws XTBusinessException {
		SupervisionEO eo = new SupervisionEO();
		BeanUtils.copyProperties(vo, eo);
		eo.setStatus(EStatus.active.getCode());
		eo.setSchedule(SupervisionScheduleEnum.no_start.getCode());
		this.save(eo);
		vo.setId(eo.getId());
		if (!XTListUtils.isEmpty(vo.getFileIds())) {
			this.fileService.updateList(vo.getFileIds(), eo.getId(), SupervisionEO.class.getName());
		}
		if (vo.getContent() != null) {
			contentService.saveOrUpdate(BusinessEnum.TYPE_LETTER.getCode(), eo.getId(), vo.getContent());
		}
		if (!XTListUtils.isEmpty(vo.getSupervisionUserList())) {
			List<Long> userIds = new ArrayList<>();
			for (SupervisionUsersEO s : vo.getSupervisionUserList()) {
				if (s.getUserId() != null) {
					userIds.add(s.getUserId());
				}
			}
			if(!XTListUtils.isEmpty(userIds)) {
				Map<Long, DeptUserVO>  map = deptUserService.getDeptByUserIds(userIds);
				if(map!=null) {
				for (SupervisionUsersEO s : vo.getSupervisionUserList()) {
					DeptUserVO dept = 	map.get(s.getUserId());
					if(dept!=null) {
						s.setDeptId(dept.getDeptId());
					}
				}
				}
			}
			for (SupervisionUsersEO s : vo.getSupervisionUserList()) {
				if (s.getUserId() != null) {
					s.setSupervisionId(eo.getId());
					supervisionUsersService.saveSelective(s);
				}
			}
		}
	}

	@Override
	@Transactional
	public void updateInfo(SupervisionVO vo) throws XTBusinessException {
		SupervisionEO eo = new SupervisionEO();
		BeanUtils.copyProperties(vo, eo);
		eo.setStatus(EStatus.active.getCode());
		this.updateByPrimaryKeySelective(eo);
		if (vo.getContent() != null) {
			contentService.saveOrUpdate(BusinessEnum.TYPE_LETTER.getCode(), eo.getId(), vo.getContent());
		}
		this.fileService.updateList(vo.getFileIds(), eo.getId(), SupervisionEO.class.getName());
		supervisionUsersService.delByPrimarySupervisionId(eo.getId());
		if (!XTListUtils.isEmpty(vo.getSupervisionUserList())) {
			List<Long> userIds = new ArrayList<>();
			for (SupervisionUsersEO s : vo.getSupervisionUserList()) {
				if (s.getUserId() != null) {
					userIds.add(s.getUserId());
				}
			}
			if(!XTListUtils.isEmpty(userIds)) {
				Map<Long, DeptUserVO>  map = deptUserService.getDeptByUserIds(userIds);
				if(map!=null) {
				for (SupervisionUsersEO s : vo.getSupervisionUserList()) {
					DeptUserVO dept = 	map.get(s.getUserId());
					if(dept!=null) {
						s.setDeptId(dept.getDeptId());
					}
				}
				}
			}
			for (SupervisionUsersEO s : vo.getSupervisionUserList()) {
				if (s.getUserId() != null) {
					s.setSupervisionId(eo.getId());
					supervisionUsersService.saveSelective(s);
				}
			}
		}
	}

	@Override
	public PagedResult<SupervisionVO> getVOPage(SupervisionVO vo) {
		Integer pageNo = vo.getPageNo() == null ? 1 : vo.getPageNo();
		Integer pageSize = vo.getPageSize() == null ? 15 : vo.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		if (XTStringUtils.isEmpty(vo.getTitle())) {
			vo.setTitle(XTSqlUtils.replaceSeparator(vo.getTitle()));
		}
		List<SupervisionEO> list = supervisionMapper.selectPagination(vo);
		PagedResult<SupervisionEO> page = PagedBeanUtil.toPagedResult(list);
		PagedResult<SupervisionVO> result = new PagedResult<>();
		BeanUtils.copyProperties(page, result);
		if (!XTListUtils.isEmpty(list)) {
			List<SupervisionVO> voList = new ArrayList<SupervisionVO>();
			for (SupervisionEO supervisionEO : list) {
				SupervisionVO supervisionVO = new SupervisionVO();
				BeanUtils.copyProperties(supervisionEO, supervisionVO);
				supervisionVO.setTypeName(SupervisionTypeEnum.getName(supervisionVO.getType()));
				supervisionVO.setUrgencyDegreeName(SupervisionLevelEnum.getName(supervisionVO.getUrgencyDegree()));
				voList.add(supervisionVO);
			}
			result.setDataList(voList);
		}
		return result;
	}

	@Override
	public SupervisionVO queryLetterDetail(Long id) throws XTBusinessException {
		SupervisionEO supervisionEO = supervisionMapper.selectByPrimaryKey(id);
		SupervisionVO supervisionVO = null;
		if (supervisionEO != null) {
			//查询上访人信息
			supervisionVO = new SupervisionVO();
			BeanUtils.copyProperties(supervisionEO, supervisionVO);
			supervisionVO.setTypeName(SupervisionTypeEnum.getName(supervisionVO.getType()));
			VisitUserEO visitUserEO = visitUserService.getByPrimaryKey(supervisionEO.getUserId());
			VisitUserVO visitUserVO = new VisitUserVO();
			BeanUtils.copyProperties(visitUserEO,visitUserVO);
			visitUserVO.setSexName(SexEnum.getName(visitUserVO.getSex()));
			SysUser sysUser = sysUserService.getByPrimaryKey(visitUserVO.getUserId());
			if(sysUser != null){
				visitUserVO.setPhone(sysUser.getPhone());
			}
			supervisionVO.setVisitUserVO(visitUserVO);

			//查询处理部门
			List<SupervisionUsersEO> supervisionUsersEOS = supervisionUsersService.getUserListBysupervisionId(supervisionVO.getId());
			if (!CollectionUtils.isEmpty(supervisionUsersEOS)) {
				List<Long> deptIds = supervisionUsersEOS.stream().map(supervisionUsersEO -> supervisionUsersEO.getDeptId()).distinct().collect(Collectors.toList());
				List<DepartmentEO> departmentEOS = departmentService.getDepartmentsByIdList(deptIds);
				if(!CollectionUtils.isEmpty(departmentEOS)){
					StringBuilder sb = new StringBuilder();
					for(DepartmentEO temp : departmentEOS){
						sb.append(temp.getName());
						sb.append("、");
					}
					String names = sb.toString();
					if(names.length()>1){
						supervisionVO.setDeptNames(names.substring(0,names.length()-1));
					}else {
						supervisionVO.setDeptNames("");
					}
				}

//				supervisionVO.setDepartmentEOS(departmentEOS);
			}

			//查询内容
			ContentEO content = contentService.queryContentByTypeAndId(BusinessEnum.TYPE_LETTER.getCode(), id);
			if (content != null) {
				supervisionVO.setContent(content.getContent());
			}

			//查询图片
			List<Long> fileIds = fileService.getFileByCaseId(id, SupervisionEO.class.getName(), null);
			if(!CollectionUtils.isEmpty(fileIds)){
				supervisionVO.setImageAction(FileUrlAdapter.assemblyAction(fileIds));
				supervisionVO.setFileIds(fileIds);
			}

//			List<FileEO> fileEOS = fileService.getFileById(id, SupervisionEO.class.getName(), null);
//			if(!CollectionUtils.isEmpty(fileEOS)){
//				supervisionVO.setFiles(fileEOS);
//			}

			//查询处理进度信息
			List<SupervisionScheduleEO> supervisionScheduleEOS = supervisionScheduleService.getListBySupervisionId(supervisionVO.getId());
			List<SupervisionScheduleVO> supervisionScheduleVOS = null;
			if (!CollectionUtils.isEmpty(supervisionScheduleEOS)) {
				supervisionScheduleVOS = new ArrayList<SupervisionScheduleVO>();
				List<Long> deptids = supervisionScheduleEOS.stream().map(supervisionScheduleEO -> supervisionScheduleEO.getDeptId()).distinct().collect(Collectors.toList());
				Map<Long, DepartmentEO> departmentEOMap = departmentService.getMapToOneByIdList(deptids);

				for (SupervisionScheduleEO supervisionScheduleEO : supervisionScheduleEOS) {
					SupervisionScheduleVO supervisionScheduleVO = new SupervisionScheduleVO();
					BeanUtils.copyProperties(supervisionScheduleEO, supervisionScheduleVO);
					DepartmentEO departmentEO = departmentEOMap.get(supervisionScheduleVO.getDeptId());
					if (departmentEO != null) {
						supervisionScheduleVO.setDeptName(departmentEO.getName());
					}

					supervisionScheduleVOS.add(supervisionScheduleVO);
				}
			}

			supervisionVO.setSupervisionScheduleVOS(supervisionScheduleVOS);
		}
		return supervisionVO;
	}

	@Override
	public List<SupervisionVO> queryLetterAll() throws XTBusinessException {
		List<SupervisionVO> supervisionVOS = null;
		SupervisionEO query = new SupervisionEO();
		query.setType(SupervisionTypeEnum.visit.getCode());
		List<SupervisionEO> supervisionEOS = supervisionMapper.selectListByCondition(query);
		if (!CollectionUtils.isEmpty(supervisionEOS)) {
			supervisionVOS = new ArrayList<SupervisionVO>();
			Map<Long,String> result = new HashMap<>();
			List<Long> supervisionIds = supervisionEOS.stream().map(supervisionEO -> supervisionEO.getId()).distinct().collect(Collectors.toList());
			//查询处理部门
			List<SupervisionUsersEO> supervisionUsersEOS = supervisionUsersService.getSupervisionUserBySupervisionIds(supervisionIds);
			if (!CollectionUtils.isEmpty(supervisionUsersEOS)) {
				List<Long> deptIds = supervisionUsersEOS.stream().map(supervisionUsersEO -> supervisionUsersEO.getDeptId()).distinct().collect(Collectors.toList());
				Map<Long,DepartmentEO> departmentEOMap = departmentService.getMapToOneByIdList(deptIds);

				for(SupervisionUsersEO temp : supervisionUsersEOS){
					DepartmentEO departmentEO = departmentEOMap.get(temp.getDeptId());
					if(result.get(temp.getSupervisionId()) == null){
						if(departmentEO != null){
							result.put(temp.getSupervisionId(),departmentEO.getName());
						}
					}else {
						String tempStr = result.get(temp.getSupervisionId());
						if(departmentEO != null){
							result.put(temp.getSupervisionId(),tempStr+"、"+departmentEO.getName());
						}
					}
				}
			}

			SupervisionVO supervisionVO = null;
			for(SupervisionEO temp : supervisionEOS){
				supervisionVO = new SupervisionVO();
				BeanUtils.copyProperties(temp,supervisionVO);
				String names = result.get(supervisionVO.getId());
				if(names != null){
					supervisionVO.setDeptNames(names);
				}else {
					supervisionVO.setDeptNames("");
				}

				if(supervisionVO.getUpdateTime() != null){
					supervisionVO.setUpdateTimeStr(sdf.format(supervisionVO.getUpdateTime()));
				}else {
					supervisionVO.setUpdateTimeStr(sdf.format(supervisionVO.getCreateTime()));
				}

				supervisionVOS.add(supervisionVO);
			}
		}
		return supervisionVOS;
	}

	@Override
	public void saveEvaluateInfo(SupervisionEstimateVO supervisionEstimateVO) throws XTBusinessException {
		SupervisionEstimateEO supervisionEstimateEO = new SupervisionEstimateEO();
		BeanUtils.copyProperties(supervisionEstimateVO,supervisionEstimateEO);
		supervisionEstimateService.save(supervisionEstimateEO);
		CommentEO commentEO = new CommentEO();
		commentEO.setBusinessId(supervisionEstimateEO.getSupervisionId());
		commentEO.setType(SupervisionTypeEnum.visit.getCode());
		commentEO.setContent(supervisionEstimateVO.getContent());
		commentEO.setUserId(supervisionEstimateVO.getUserId());
		commentService.save(commentEO);

	}

	@Override
	public SupervisionEstimateVO queryLetterEvaluateDetail(Long id, Long userId) {
		SupervisionEstimateVO supervisionEstimateVO = null;
		SupervisionEstimateEO supervisionEstimateEO = new SupervisionEstimateEO();
		supervisionEstimateEO.setSupervisionId(id);
//		supervisionEstimateEO.setUserId(userId);
		List<SupervisionEstimateEO> supervisionEstimateEOS = supervisionEstimateService.getChcekInfoByCondition(supervisionEstimateEO);
		if (!CollectionUtils.isEmpty(supervisionEstimateEOS)) {
			supervisionEstimateVO = new SupervisionEstimateVO();
			SupervisionEstimateEO supervisionEstimateEO1 = supervisionEstimateEOS.get(0);
			BeanUtils.copyProperties(supervisionEstimateEO1, supervisionEstimateVO);
			supervisionEstimateVO.setEstimateName(SupervisionEstimateEnum.getName(supervisionEstimateVO.getEstimate()));

			CommentEO commentEO = commentService.selectCommentSingle(supervisionEstimateVO);
			if(commentEO != null){
				supervisionEstimateVO.setContent(commentEO.getContent());
			}else {
				supervisionEstimateVO.setContent("");
			}
			return supervisionEstimateVO;
		}

		return null;
	}

	@Override
	public Integer getUserLetterCount(Long userId) {
		VisitUserVO visitUserVO = visitUserService.getInfo(userId);
		if (visitUserVO != null) {
			return visitUserVO.getNumber();
		}
		return 0;
	}

	@Override
	public List<SupervisionVO> getUserLetterInfos(Long userId) {
		List<SupervisionVO> supervisionVOS = null;
		SupervisionEO querySupervision = new SupervisionEO();
		querySupervision.setUserId(userId);
		querySupervision.setType(SupervisionTypeEnum.visit.getCode());
		List<SupervisionEO> supervisionEOS = supervisionMapper.selectListByCondition(querySupervision);
		if (!CollectionUtils.isEmpty(supervisionEOS)) {
			List<Long> supervisionIds = supervisionEOS.stream().map(supervisionEO -> supervisionEO.getId()).distinct().collect(Collectors.toList());
			List<SupervisionScheduleEO> supervisionScheduleEOS = supervisionScheduleService.getListBySupervisionIds(supervisionIds);
			Map<Long,SupervisionScheduleVO> supervisionScheduleVOMap = null;

			if (!CollectionUtils.isEmpty(supervisionScheduleEOS)) {
				List<Long> deptIds = supervisionScheduleEOS.stream().map(supervisionScheduleEO -> supervisionScheduleEO.getDeptId()).distinct().collect(Collectors.toList());
				Map<Long, DepartmentEO> departmentEOMap = departmentService.getMapToOneByIdList(deptIds);
				supervisionScheduleVOMap = new HashMap<Long, SupervisionScheduleVO>();
				SupervisionScheduleVO supervisionScheduleVO = null;
				for (SupervisionScheduleEO supervisionScheduleEO : supervisionScheduleEOS) {
					supervisionScheduleVO = new SupervisionScheduleVO();
					BeanUtils.copyProperties(supervisionScheduleEO,supervisionScheduleVO);
					DepartmentEO departmentEO = departmentEOMap.get(supervisionScheduleVO.getDeptId());
					if(departmentEO != null){
						supervisionScheduleVO.setDeptName(departmentEO.getName());
					}

					if(!supervisionScheduleVOMap.containsKey(supervisionScheduleVO.getSupervisionId())){
						supervisionScheduleVOMap.put(supervisionScheduleVO.getSupervisionId(),supervisionScheduleVO);
					}

				}

			}

			supervisionVOS = new ArrayList<SupervisionVO>();
			SupervisionVO supervisionVO = null;
			for(SupervisionEO supervisionEO:supervisionEOS){
				supervisionVO = new SupervisionVO();
				BeanUtils.copyProperties(supervisionEO,supervisionVO);
				supervisionVO.setScheduleName(SupervisionScheduleEnum.getName(supervisionVO.getSchedule()));
				if(supervisionScheduleVOMap != null ){
					if(supervisionScheduleVOMap.get(supervisionVO.getId()) != null){
						supervisionVO.setDeptNames(supervisionScheduleVOMap.get(supervisionVO.getId()).getDeptName());

					}
				}


				supervisionVOS.add(supervisionVO);
			}

		}

		return supervisionVOS;
	}

	@Override
	public SupervisionVO getInfo(Long id) throws XTBusinessException {
		SupervisionEO wd = super.getByPrimaryKey(id);
		if (wd != null) {
			SupervisionVO vo = new SupervisionVO();
			BeanUtils.copyProperties(wd, vo);
			vo.setUrgencyDegreeName(SupervisionLevelEnum.getName(vo.getUrgencyDegree()));
			vo.setTypeName(SupervisionTypeEnum.getName(vo.getType()));
			List<SupervisionUsersEO> list = supervisionUsersService.getUserListBysupervisionId(wd.getId());
			vo.setSupervisionUserList(list);
			List<FileEO> files = this.fileService.getFileById(id, SupervisionEO.class.getName(), null);
			vo.setFiles(files);
			ContentEO content = contentService.queryContentByTypeAndId(BusinessEnum.TYPE_LETTER.getCode(), id);
			if(content!=null) {
				vo.setContent(content.getContent());
			}
			return vo;
		}

		return null;
	}
	
	@Override
	public SupervisionVO getAPIInfo(Long id) throws XTBusinessException {
		SupervisionEO wd = super.getByPrimaryKey(id);
		if (wd != null) {
			SupervisionVO vo = new SupervisionVO();
			BeanUtils.copyProperties(wd, vo);
			vo.setUrgencyDegreeName(SupervisionLevelEnum.getName(vo.getUrgencyDegree()));
			vo.setTypeName(SupervisionTypeEnum.getName(vo.getType()));
			List<SupervisionUsersEO> list = supervisionUsersService.getUserListBysupervisionId(wd.getId());
			vo.setSupervisionUserList(list);
			List<FileEO> files = this.fileService.getFileById(id, SupervisionEO.class.getName(), null);
			vo.setFiles(files);
			if(!XTListUtils.isEmpty(files)) {
				vo.setFileIds(XTEntityUtils.getIdList(files));
			}
			ContentEO content = contentService.queryContentByTypeAndId(BusinessEnum.TYPE_LETTER.getCode(), id);
			if(content!=null) {
				vo.setContent(content.getContent());
			}
			return vo;
		}
		
		return null;
	}


	public void getWorks(StatisticsVO vo) {
		List<SupervisionEO> list =	supervisionMapper.selectByStatistics(vo);
		if(XTListUtils.isEmpty(list)) {
			return;
		}
		Integer all = 0;
		Integer internal = 0;
		Integer visit = 0;
		Integer complete_internal = 0;
		Integer complete_visit = 0;
		Integer all_overdue = 0;
		for (SupervisionEO eo : list) {
			if(eo.getSchedule() != null && SupervisionScheduleEnum.endIng.getCode().equals(eo.getSchedule())){
				all+=1;
			}

			if(eo.getCompleteTime() != null && eo.getUpdateTime() != null){
				if(eo.getCompleteTime().getTime() < eo.getUpdateTime().getTime()){
					all_overdue+=1;
				}
			}
			if(eo.getType().equals(SupervisionTypeEnum.internal.getCode())) {
				internal+=1;
				if(SupervisionScheduleEnum.endIng.getCode().equals(eo.getSchedule())) {
					complete_internal +=1; 
				}
			}else {
				visit+=1;
				if(SupervisionScheduleEnum.endIng.getCode().equals(eo.getSchedule())) {
					complete_visit +=1; 
				}
			}
		}
		//工作情况
		vo.setWork_all(all);
		vo.setWork_internal(internal);
		vo.setWork_visit(visit);
		//完成情况
		vo.setComplete_all(all);
		vo.setComplete_internal(complete_internal);
		vo.setComplete_visit(complete_visit);
		//逾期
		vo.setComplete_all_overdue(all_overdue);
	}

	@Override
	public PagedResult<SupervisionVO> getLetterPage(SupervisionEO eo) {
	
		Integer pageNo = eo.getPageNo() == null ? 1 : eo.getPageNo();
		Integer pageSize = eo.getPageSize() == null ? 15 : eo.getPageSize();
		PageHelper.startPage(pageNo, pageSize);
		eo.setType(SupervisionTypeEnum.visit.getCode());
		if (XTStringUtils.isEmpty(eo.getTitle())) {
			eo.setTitle(XTSqlUtils.replaceSeparator(eo.getTitle()));
		}
		List<SupervisionEO> list = supervisionMapper.selectPagination(eo);
		PagedResult<SupervisionEO> page = PagedBeanUtil.toPagedResult(list);
		PagedResult<SupervisionVO> result = new PagedResult<>();
		BeanUtils.copyProperties(page, result);
		if (!XTListUtils.isEmpty(list)) {
			List<Long> ids = XTEntityUtils.getIdList(list);
			Map<Long, List<FileEO>> fileMap = this.fileService.getListToMap(ids, SupervisionEO.class.getName(),null);
			Map<Long, SupervisionEstimateEO> esMap = supervisionEstimateService.getMapByIdList(ids);
			List<SupervisionVO> voList = new ArrayList<SupervisionVO>();
			for (SupervisionEO supervisionEO : list) {
				SupervisionVO supervisionVO = new SupervisionVO();
				BeanUtils.copyProperties(supervisionEO, supervisionVO);
				if(fileMap!=null) {
					List<FileEO> files = fileMap.get(supervisionEO.getId());
					if(!XTListUtils.isEmpty(files)) {
						supervisionVO.setFiles(files);
					}
				}
				if(esMap!=null) {
					SupervisionEstimateEO est =	esMap.get(supervisionEO.getId());
					if(est!=null) {
						supervisionVO.setSatisfaction(SupervisionEstimateEnum.getName(est.getEstimate()));
					}
				}
				supervisionVO.setScheduleName(SupervisionScheduleEnum.getName(supervisionVO.getSchedule()));
				voList.add(supervisionVO);
			}
			result.setDataList(voList);
		}
		return result;
	}


	@Override
	public List<SupervisionVO> getSupervisionListToMe(Long userId) {
		List<SupervisionEO> supervisionEOS = supervisionMapper.getSupervisionIdBySupervisionUserId(userId);

		List<SupervisionVO> supervisionVOS = null;
		if(!CollectionUtils.isEmpty(supervisionEOS)){
			List<Long> userIds = supervisionEOS.stream().map(supervisionEO -> supervisionEO.getUserId()).distinct().collect(Collectors.toList());
			Map<Long, SysUser> userMap = sysUserService.getMapTopOne(userIds);

			//根据用户id查询当前被分配的督办的进度
			List<SupervisionScheduleEO> supervisionScheduleEOS = supervisionScheduleService.getListByUserId(userId);
			Map<Long, SupervisionScheduleEO> scheduleMap = null;
			if (!CollectionUtils.isEmpty(supervisionScheduleEOS)) {
				scheduleMap = new HashMap<Long, SupervisionScheduleEO>();
				for (SupervisionScheduleEO scheduleTemp : supervisionScheduleEOS) {
					if (scheduleMap.get(scheduleTemp.getSupervisionId()) == null) {
						scheduleMap.put(scheduleTemp.getSupervisionId(), scheduleTemp);
					}
				}
			}
			supervisionVOS = new ArrayList<SupervisionVO>();
			SupervisionVO supervisionVO = null;
			for(SupervisionEO temp : supervisionEOS){
				supervisionVO = new SupervisionVO();
				BeanUtils.copyProperties(temp,supervisionVO);
				supervisionVO.setUrgencyDegreeName(SupervisionLevelEnum.getName(supervisionVO.getUrgencyDegree()));
				supervisionVO.setScheduleName(SupervisionScheduleEnum.getName(supervisionVO.getSchedule()));
				supervisionVO.setTypeName(SupervisionTypeEnum.getName(supervisionVO.getType()));
				SysUser sysUser = userMap.get(supervisionVO.getUserId());
				if(sysUser != null){
					supervisionVO.setUserName(sysUser.getNickname());
				}
				if(scheduleMap!=null) {
				SupervisionScheduleEO supervisionScheduleEO = scheduleMap.get(supervisionVO.getId());
				if(supervisionScheduleEO != null){
					supervisionVO.setSchedulePercent(supervisionScheduleEO.getSchedule());
				}
				}
				if(supervisionVO.getSchedulePercent()==null) {
					supervisionVO.setSchedulePercent(0);
				}
				supervisionVOS.add(supervisionVO);
			}
		}
		return supervisionVOS;
	}

	@Override
	public SupervisionVO querySupervisionScheduleDetail(Long supervisionId, Long userId) throws XTBusinessException{
		SupervisionEO supervisionEO = supervisionMapper.selectByPrimaryKey(supervisionId);
		SupervisionVO supervisionVO = null;
		SimpleDateFormat sdf = new SimpleDateFormat("MM-dd  yyyy");
		if(supervisionEO != null){
			supervisionVO = new SupervisionVO();
			BeanUtils.copyProperties(supervisionEO,supervisionVO);
			SysUser sysUser = sysUserService.getByPrimaryKey(userId);
			supervisionVO.setUrgencyDegreeName(SupervisionLevelEnum.getName(supervisionVO.getUrgencyDegree()));
			supervisionVO.setScheduleName(SupervisionScheduleEnum.getName(supervisionVO.getSchedule()));
			supervisionVO.setTypeName(SupervisionTypeEnum.getName(supervisionVO.getType()));
			supervisionVO.setUserName(sysUser.getNickname());

			//查询上传的附件
			List<FileEO> files = fileService.getFileById(supervisionId,SupervisionEO.class.getName(),null);
			if(!CollectionUtils.isEmpty(files)){
				supervisionVO.setLetterNum(files.size());
//				supervisionVO.setFiles(files);
			}
			List<SupervisionScheduleEO> supervisionScheduleEOS = supervisionScheduleService.getListBySupervisionIdAndUserId(supervisionId,userId);
			List<SupervisionScheduleVO> supervisionScheduleVOS = null;
			if(!CollectionUtils.isEmpty(supervisionScheduleEOS)){
				supervisionScheduleVOS = new ArrayList<SupervisionScheduleVO>();
				SupervisionScheduleEO temp = supervisionScheduleEOS.get(0);
				DepartmentEO departmentEO = departmentService.getByPrimaryKey(temp.getDeptId());
				for(SupervisionScheduleEO t : supervisionScheduleEOS){
					SupervisionScheduleVO supervisionScheduleVO = new SupervisionScheduleVO();
					BeanUtils.copyProperties(t,supervisionScheduleVO);
					supervisionScheduleVO.setNickname(XTCurrentUser.getCurrentUser().getNickname());
					supervisionScheduleVO.setDeptName(departmentEO.getName());
					if(supervisionScheduleVO.getTime()!=null){
						supervisionScheduleVO.setReportTime(sdf.format(supervisionScheduleVO.getTime()));
					}
					supervisionScheduleVOS.add(supervisionScheduleVO);
				}

			}
			supervisionVO.setSupervisionScheduleVOS(supervisionScheduleVOS);
		}
		return supervisionVO;
	}

	@Override
	public List<SupervisionVO> getSupervisionLetteringList() {
		List<SupervisionEO> supervisionEOS = supervisionMapper.selectAll();
		List<SupervisionVO> supervisionVOS = null;
		if(!CollectionUtils.isEmpty(supervisionEOS)){
			supervisionVOS = new ArrayList<SupervisionVO>();
			SupervisionVO supervisionVO = null;
			for(SupervisionEO temp : supervisionEOS){
				if(SupervisionTypeEnum.visit.getCode().equals(temp.getType()) && SupervisionScheduleEnum.startIng.getCode().equals(temp.getSchedule())){
					supervisionVO = new SupervisionVO();
					BeanUtils.copyProperties(temp,supervisionVO);
					supervisionVO.setScheduleName(SupervisionScheduleEnum.getName(supervisionVO.getSchedule()));
					if(supervisionVO.getUpdateTime() != null){
						supervisionVO.setUpdateTimeStr(sdf.format(supervisionVO.getUpdateTime()));
					}else {
						supervisionVO.setUpdateTimeStr(sdf.format(supervisionVO.getCreateTime()));
					}
					supervisionVOS.add(supervisionVO);
				}

			}

			//查询处理部门
			List<Long> ids = supervisionVOS.stream().map(supervisionVO1 -> supervisionVO1.getId()).distinct().collect(Collectors.toList());
			if(!CollectionUtils.isEmpty(ids)){
				List<SupervisionUsersEO> supervisionUsersEOS = supervisionUsersService.getSupervisionUserBySupervisionIds(ids);
				Map<Long,Set<DepartmentEO>> supervisionDepartmentMap = null;
				if (!CollectionUtils.isEmpty(supervisionUsersEOS)) {
					supervisionDepartmentMap = new HashMap<>();
					List<Long> deptIds = supervisionUsersEOS.stream().map(supervisionUsersEO -> supervisionUsersEO.getDeptId()).distinct().collect(Collectors.toList());
					Map<Long,DepartmentEO> departmentEOMap = departmentService.getMapToOneByIdList(deptIds);
					for(SupervisionUsersEO temp : supervisionUsersEOS){
						if(supervisionDepartmentMap.get(temp.getSupervisionId()) == null){
							Set<DepartmentEO> departmentEOS = new HashSet<>();
							departmentEOS.add(departmentEOMap.get(temp.getDeptId()));
							supervisionDepartmentMap.put(temp.getSupervisionId(),departmentEOS);

						}else {
							Set<DepartmentEO> departmentEOS = supervisionDepartmentMap.get(temp.getSupervisionId());
							departmentEOS.add(departmentEOMap.get(temp.getDeptId()));
						}
					}
				}

				for(SupervisionVO temp : supervisionVOS){
					List<DepartmentEO> departmentEOS = new ArrayList<>();
					if(supervisionDepartmentMap.get(temp.getId()) != null){
						departmentEOS.addAll(supervisionDepartmentMap.get(temp.getId()));
					}

					StringBuilder sb = new StringBuilder();
					for(DepartmentEO d : departmentEOS){
						sb.append(d.getName());
						sb.append("、");
					}
					String sbStr = sb.toString();
					if(sbStr.length() > 0){
						temp.setDeptNames(sbStr.substring(0,sbStr.length()-1));
					}else {
						temp.setDeptNames("");
					}
					temp.setDepartmentEOS(departmentEOS);

				}
			}

		}


		return supervisionVOS;
	}

	@Override
	public List<SupervisionVO> getSupervisionLetteredList() {
		List<SupervisionEO> supervisionEOS = supervisionMapper.selectAll();
		List<SupervisionVO> supervisionVOS = null;
		if(!CollectionUtils.isEmpty(supervisionEOS)){
			supervisionVOS = new ArrayList<SupervisionVO>();
			SupervisionVO supervisionVO = null;
			for(SupervisionEO temp : supervisionEOS){
				if(SupervisionTypeEnum.visit.getCode().equals(temp.getType()) && SupervisionScheduleEnum.endIng.getCode().equals(temp.getSchedule())){
					supervisionVO = new SupervisionVO();
					BeanUtils.copyProperties(temp,supervisionVO);
					supervisionVO.setScheduleName(SupervisionScheduleEnum.getName(supervisionVO.getSchedule()));
					if(supervisionVO.getUpdateTime() != null){
						supervisionVO.setUpdateTimeStr(sdf.format(supervisionVO.getUpdateTime()));
					}else {
						supervisionVO.setUpdateTimeStr(sdf.format(supervisionVO.getCreateTime()));
					}
					supervisionVOS.add(supervisionVO);
				}

			}

			//查询处理部门
			List<Long> ids = supervisionVOS.stream().map(supervisionVO1 -> supervisionVO1.getId()).distinct().collect(Collectors.toList());
			if(!CollectionUtils.isEmpty(ids)){
				List<SupervisionUsersEO> supervisionUsersEOS = supervisionUsersService.getSupervisionUserBySupervisionIds(ids);
				Map<Long,Set<DepartmentEO>> supervisionDepartmentMap = null;
				if (!CollectionUtils.isEmpty(supervisionUsersEOS)) {
					supervisionDepartmentMap = new HashMap<>();
					List<Long> deptIds = supervisionUsersEOS.stream().map(supervisionUsersEO -> supervisionUsersEO.getDeptId()).distinct().collect(Collectors.toList());
					Map<Long,DepartmentEO> departmentEOMap = departmentService.getMapToOneByIdList(deptIds);
					for(SupervisionUsersEO temp : supervisionUsersEOS){
						if(supervisionDepartmentMap.get(temp.getSupervisionId()) == null){
							Set<DepartmentEO> departmentEOS = new HashSet<>();
							departmentEOS.add(departmentEOMap.get(temp.getDeptId()));
							supervisionDepartmentMap.put(temp.getSupervisionId(),departmentEOS);

						}else {
							Set<DepartmentEO> departmentEOS = supervisionDepartmentMap.get(temp.getSupervisionId());
							departmentEOS.add(departmentEOMap.get(temp.getDeptId()));
						}
					}
				}

				for(SupervisionVO temp : supervisionVOS){
					List<DepartmentEO> departmentEOS = new ArrayList<>();
					if(supervisionDepartmentMap.get(temp.getId()) != null){
						departmentEOS.addAll(supervisionDepartmentMap.get(temp.getId()));
					}
					temp.setDepartmentEOS(departmentEOS);
					StringBuilder sb = new StringBuilder();
					for(DepartmentEO d : departmentEOS){
						sb.append(d.getName());
						sb.append("、");
					}
					String sbStr = sb.toString();
					if(sbStr.length() > 0){
						temp.setDeptNames(sbStr.substring(0,sbStr.length()-1));
					}else {
						temp.setDeptNames("");
					}
					temp.setDepartmentEOS(departmentEOS);

				}
			}

		}


		return supervisionVOS;
	}

	@Override
	public List<SupervisionVO> getSupervisionListFromMe(Long userId) {
		List<SupervisionEO> supervisionEOS = supervisionMapper.selectByUserId(userId);
		List<SupervisionVO> supervisionVOS = null;
		if(!CollectionUtils.isEmpty(supervisionEOS)){
			supervisionVOS = new ArrayList<SupervisionVO>();
			SupervisionVO supervisionVO = null;
			for(SupervisionEO temp : supervisionEOS){
				supervisionVO = new SupervisionVO();
				BeanUtils.copyProperties(temp,supervisionVO);
				supervisionVO.setUrgencyDegreeName(SupervisionLevelEnum.getName(supervisionVO.getUrgencyDegree()));
				supervisionVO.setScheduleName(SupervisionScheduleEnum.getName(supervisionVO.getSchedule()));
				supervisionVO.setTypeName(SupervisionTypeEnum.getName(supervisionVO.getType()));
				supervisionVOS.add(supervisionVO);
			}
		}
		return supervisionVOS;
	}


	@Override
	public void getStatistics(StatisticsVO vo) {
		//工作量 and 完成情况
		this.getWorks(vo);
		// 满意度
		supervisionEstimateService.getQualitys(vo);
	}

	@Override
	public void deleteSupervision(Long id) {
		SupervisionEO supervisionEO = new SupervisionEO();
		supervisionEO.setIsDel("1");
		supervisionEO.setId(id);
		supervisionMapper.updateByPrimaryKey(supervisionEO);
	}


	@Override
	public List<SupervisionEO> getNoStartLetteringList() {
		List<SupervisionEO> list =	supervisionMapper.selectNoStartLetteringList();
		return list;
	}
	
	
	
}
